﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.Entity;

namespace CMS.DAL
{
    /// <summary>
    /// 年报
    /// </summary>
    public class YearProfileDAL
    {
        public YearProfile GetOne(string stock, DateTime endDate)
        {
            string sql = "select * from  dbo.YearProfile where Stock=@Stock and EndDate=@EndDate";
            Tools tool = new Tools();
            tool.AddSqlParameter("Stock", System.Data.SqlDbType.VarChar, stock);
            tool.AddSqlParameter("EndDate", System.Data.SqlDbType.DateTime, endDate);
            return SqlHelper.GetDataByColumns<YearProfile>(DBConn.gaInfo, sql, tool.GetSqlParameters());
        }

        public void Update(YearProfile c)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("UPDATE [gaInfo].[dbo].[YearProfile]");
            sb.AppendLine("   SET [BussinessProfileFlag] = @BussinessProfileFlag");
            sb.AppendLine("      ,[BussinessProfileListFlag] = @BussinessProfileListFlag");
            sb.AppendLine("      ,[RecTime] = @RecTime");
            sb.AppendLine("      ,[ModTime] = @ModTime");
            sb.AppendLine(" WHERE [Stock]=@Stock and [EndDate]=@EndDate");
            var sps=SqlHelper.ObjectToSqlParameters(c,new string[] {"Content"});
            SqlHelper.ExecuteNonQuery(DBConn.gaInfo, System.Data.CommandType.Text,sb.ToString(),sps );
        }

        public List<YearProfile> GetAll()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("select [Stock],[EndDate],[BussinessProfileFlag],[BussinessProfileListFlag],[RecTime],[ModTime] from  YearProfile where stock+cast(enddate as varchar)in(");
            sb.AppendLine("select stock+cast(MAX(EndDate) as varchar) from dbo.YearProfile group by stock)");
            sb.AppendLine("order by EndDate desc");
            return SqlHelper.GetDataByColumnsLst<YearProfile>(DBConn.gaInfo, sb.ToString(), null);
        }

        public List<YearProfile> GetLst(string stock)
        {
            string sql = "select top 1 * from YearProfile where stock = @stock order by EndDate desc";
            Tools tool = new Tools();
            tool.AddSqlParameter("stock", System.Data.SqlDbType.VarChar, stock);
            return SqlHelper.GetDataByColumnsLst<YearProfile>(DBConn.gaInfo, sql, tool.GetSqlParameters());
        }
    }
}
